package com.tjetc.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjetc.common.JsonResult;
import com.tjetc.dao.BedInfoMapper;
import com.tjetc.dao.ElderInfoMapper;
import com.tjetc.entity.BedInfo;
import com.tjetc.entity.ElderInfo;
import com.tjetc.param.BedCount;
import com.tjetc.service.BedInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
@Service
public class BedInfoServiceImpl implements BedInfoService {
    @Autowired
    private BedInfoMapper bedInfoMapper;
    @Autowired
    private ElderInfoMapper elderInfoMapper;
    @Override
    public JsonResult findById(Long id) {
        BedInfo bedInfo=bedInfoMapper.selectById(id);
        //selectById是mybatisplus提供的单表查询操作，生成sql语句都是mybatisplus内置
        ElderInfo elderInfo = elderInfoMapper.selectById(bedInfoMapper.selectElderlyId(id));
        bedInfo.setElderly(elderInfo);
        return JsonResult.success(bedInfo);
    }
    @Override
    public JsonResult findPage(Integer wardId,Integer status, Integer pageNo, Integer pageSize) {
        //设置页码和每页数量
        Page<BedInfo> page=Page.of(pageNo,pageSize);
        //把adminPage传入mapper查询中，adminPage就是最后的分页结果
        Page<BedInfo>bedInfoPage=null;
        if(wardId==null&&status==null)bedInfoPage= bedInfoMapper.selectPageAll(page);
        else bedInfoPage= bedInfoMapper.selectPageWardID(page,wardId,status);
        for (BedInfo bedInfo : bedInfoPage.getRecords()) {
            ElderInfo elderInfo = elderInfoMapper.selectById(bedInfoMapper.selectElderlyId(bedInfo.getBedId()));
            bedInfo.setElderly(elderInfo);
            bedInfo.setElderMap(bedInfoMapper.selectElderMap());
        }
        //convert方法转换Page中 records集合元素类型 Admin---->AdminView
        //new Function<Admin,AdminView>(){};表示创建一个实现Function的匿名类 对象
        return JsonResult.success(bedInfoPage);
    }
    @Override
    public JsonResult deleteById(Long id) {
        int rows=bedInfoMapper.deleteById(id);
        if (rows>0){
            return JsonResult.success("删除成功");
        }
        else {
            return JsonResult.fail("删除失败");
        }
    }
    //@Transactional用在service方法上，保证事务
    //注意：默认情况@Transactional只对运行时异常起作用(RuntimeException)
    @Transactional
    @Override
    public JsonResult save(BedInfo bedInfoParam) {
        BedInfo bedInfo=null;
        LocalDateTime now=LocalDateTime.now();
        //通过判断id值有无判断是新增还是更新，id值无是新增，id值有就是更新
        if(bedInfoParam.getBedId()==null||bedInfoParam.getBedId()<=0){
            //新增
            bedInfo=new BedInfo();
            BeanUtils.copyProperties(bedInfoParam,bedInfo);
            bedInfo.setCreateTime(now);
            bedInfo.setUpdateTime(now);
            bedInfoMapper.insert(bedInfo);
            return JsonResult.success("新增成功",null);
        }else{
            //更新
            //根据id查询Admin信息
            bedInfo=bedInfoMapper.selectById(bedInfoParam.getBedId());
            if(bedInfo==null){
                return JsonResult.fail("用户不存在，更新失败");
            }
            //要更新的字段
            bedInfo.setUpdateTime(now);
            bedInfo.setBedNumber(bedInfoParam.getBedNumber());
            bedInfo.setElderly(bedInfoParam.getElderly());
            bedInfo.setElderlyId(bedInfoParam.getElderlyId());
            System.out.println("bedInfoParam.getElderlyId() = " + bedInfoParam.getElderlyId());
            bedInfo.setFloor(bedInfoParam.getFloor());
            bedInfo.setIsDeleted(bedInfoParam.getIsDeleted());
            bedInfo.setPrice(bedInfoParam.getPrice());
            bedInfo.setStatus(bedInfoParam.getStatus());
            bedInfo.setWardId(bedInfoParam.getWardId());
            int rows=bedInfoMapper.updateById(bedInfo);
            if (rows>0){
                return JsonResult.success("更新成功",null);
            }else {
                return JsonResult.fail("用户不存在，更新失败");
            }
        }
    }

    @Override
    public JsonResult selectBedNumber() {
        BedCount bedCount=new BedCount();
        bedCount.setBedOccupy(bedInfoMapper.selectBedCountOccupy());
        bedCount.setBedNotOccupy(bedInfoMapper.selectBedCountNotOccupy());
        bedCount.setWardNumber(bedInfoMapper.selectBedCountWard());
        return JsonResult.success(bedCount);
    }
}
